package com.delong.spring.handler.app;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.delong.cache.AppCache;
import com.delong.model.app.App;
import com.delong.model.app.Company;
import com.delong.service.AppService;
import com.delong.service.CompanyService;
import com.delong.spring.handler.BaseController;

/**
 * 厂商管理
 * 
 * @author liyang
 *
 */

@Controller
@RequestMapping("/company")
public class CompanyController extends BaseController {

	private CompanyService service = new CompanyService();
	AppService appService = new AppService();

	@RequestMapping("/list")
	public String getAppCategoryLit(Map<String, Object> model, ServletRequest req) {
		// HttpServletRequest request = (HttpServletRequest) req;
		model.put("username", this.getUser().getUsername());
		return "app/Company";
	}

	@RequestMapping("/getall")
	@ResponseBody
	public String getAllElement(Map<String, Object> model, ServletRequest req) {
		CompanyService companyService = new CompanyService();
		List<Company> companys = companyService.getAllList();
		JSONObject result = new JSONObject();
		result.put("rows", companys);
		result.put("code", 0);
		return result.toJSONString();
	}

	@RequestMapping("/getdata")
	@ResponseBody
	public String getData(Map<String, Object> model,
			@RequestParam(value = "companyName", defaultValue = "") String companyName,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "15") int rows) {
		JSONObject result = new JSONObject();
		if (!companyName.trim().equals("")) {
			companyName = "%" + companyName + "%";
		}
		List<Company> elements = service.getListElementByName(companyName, (page - 1) * rows, rows);
		int total = service.getCountByName(companyName);
		result.put("code", 0);
		result.put("rows", elements);
		result.put("total", total);
		return result.toJSONString();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String addElement(@RequestParam(value = "companyName", defaultValue = "") String companyName,
			@RequestParam(value = "enName", defaultValue = "") String enName,
			@RequestParam(value = "region", defaultValue = "0") int region,
			@RequestParam(value = "id", defaultValue = "0") int id) {

		Company element = new Company();
		element.setCompanyName(companyName);
		element.setRegion(region);
		element.setEnName(enName);
		element.setId(id);
		if (id == 0) {
			element.setCreatetime(new Date());
			this.service.addElement(element);
		} else {
			this.service.changeElement(element);
			List<App> appElements = this.appService.getListByCompanyId(id);
			AppCache cache = new AppCache();
			cache.deleteApps(appElements);
		}
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("msg", "success");
		return result.toJSONString();
	}

	@RequestMapping(value = "/getelement")
	@ResponseBody
	public String getElement(@RequestParam(value = "id", defaultValue = "0") int id) {
		Company element = this.service.getElement(id);
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("data", element);
		return result.toJSONString();
	}

	@RequestMapping(value = "/changestatus")
	@ResponseBody
	public String changeStatus(@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "status", defaultValue = "0") int status) {
		this.service.changeStatus(status, id);
		if (status == 1) {
			List<App> appElements = this.appService.getListByCompanyId(id);
			AppCache cache = new AppCache();
			cache.deleteApps(appElements);
		}
		JSONObject result = new JSONObject();
		result.put("code", 0);
		return result.toJSONString();
	}

}
